#include "GameManager.h"
#include "GamePlatform.h"

#include <QApplication>
#include <QProcess>
#include <QResource>
#include <QFont>
#include <QFontDatabase>
#include <QMessageBox>

void customReleaseMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    QString text;

    switch(type)
    {
    case QtDebugMsg:
        return;
        text = QString("Debug:");
        break;

    case QtWarningMsg:
        return;
        text = QString("Warning:");
        break;

    case QtCriticalMsg:
        text = QString("Critical:");
        break;

    case QtFatalMsg:
        text = QString("Fatal:");
        break;

    default:
        text = QString("Unkown:");
        break;
    }


    QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
    QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
    QString current_date = QString("(%1)").arg(current_date_time);
    QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);


    mutex.lock();

    QFile file(QString(FIXLOCALPATH_W("log_%1.txt")).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd")));
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream text_stream(&file);
    text_stream << message << "\r\n";
    file.flush();
    file.close();

    mutex.unlock();
}

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);


#if defined(Q_OS_WIN32)
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    qInstallMessageHandler(customReleaseMessageHandler);
#else
    //qInstallMessageHandler(customMessageHandler);
#endif


#elif defined(Q_OS_ANDROID)

#ifdef QT_NO_DEBUG
    qInstallMessageHandler(customAndroidMessageHandler);
#else
    qInstallMessageHandler(customMessageHandler);
#endif


#elif defined(Q_OS_LINUX)
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    qInstallMessageHandler(customReleaseMessageHandler);
#else
    qInstallMessageHandler(customMessageHandler);
#endif



#elif defined(Q_OS_MACOS)
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    qInstallMessageHandler(customReleaseMessageHandler);
#else
    qInstallMessageHandler(customMessageHandler);
#endif



#elif defined(Q_OS_IOS)
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    qInstallMessageHandler(customReleaseMessageHandler);
#else
    qInstallMessageHandler(customMessageHandler);
#endif



#else
#ifdef QT_NO_DEBUG
    //在程序开始前添加
    //qInstallMessageHandler(customReleaseMessageHandler);
#else
    qInstallMessageHandler(customMessageHandler);
#endif



#endif



    //QCoreApplication::applicationDirPath()


    //QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);   //绘图方式
    qputenv( "QSG_RENDER_LOOP", "basic" );  //单线程绘图,解决qml过快问题

    QApplication app(argc, argv);


//载入字体
    QFont font;
    int fontId = QFontDatabase::addApplicationFont(FIXLOCALPATH_R("Media/Fonts/Font1.ttf"));
    if (fontId != -1)
    {
        QStringList strList(QFontDatabase::applicationFontFamilies(fontId));
        if (strList.count() > 0)
        {
            font.setFamily(strList.at(0));
            qDebug() << "字体载入成功:" << strList.at(0);
        }
    }
    else
        font.setFamily("黑体");

    app.setFont(font);
    //font.setPointSize(16);
    qDebug() << "字体:" << font.family();


    initialization();   //启动程序前初始化



#if defined(Q_OS_WIN32)

#ifdef QT_NO_DEBUG
    if(!checkOne(L"Pleafles_EnglishKill"))
    {
        qDebug() << "已经运行!";
        return -10;
    }

    if(argc < 3 || QString("-s") != argv[1] || QString("Pleafles") != argv[2])
    {
        QProcess::startDetached("EnglishKill.exe");
        return -11;
    }


#else

#endif


#elif defined(Q_OS_ANDROID)
    keepScreenWake(true);



#elif defined(Q_OS_LINUX)



#elif defined(Q_OS_MACOS)



#elif defined(Q_OS_IOS)



#else



#endif


    //Q_INIT_RESOURCE
    //Q_CLEANUP_RESOURCE()
    //从rcc文件中载入qml文件和其他资源(可以载入多个,引用方式为qrc:/...)
    bool ret = QResource::registerResource(FIXLOCALPATH_R("QML.dat")) &
            QResource::registerResource(FIXLOCALPATH_R("Res.dat"));
    if(!ret)
    {
        QMessageBox::information(nullptr,"失败","载入资源文件失败!");
        qCritical()<<"载入资源文件失败!";
        return -2;
    }


    GameManager manager;
    if(!manager.InitOnce())
        return -1;

    return app.exec();
}
